package com.base.cn.platform.os.controller.course.book;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.service.course.book.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
/**
 * 后台图书管理Controller
 *
 * @author s.li
 * @create 2018-05-08-14:55
 */
@Controller
@RequestMapping("/admin/book")
public class AdminBookController extends AdminBaseController {

    @Autowired
    private BookService bookService;

    /**
     * 图书列表
     * @return
     */
    @RequestMapping("/bookList")
    public String bookList(){
        return resultPage("/course/book/book_list");
    }


    /**
     * 图书列表
     * @return
     */
    @RequestMapping("/selectBookList")
    public String selectBookList(Model model,
                                 @RequestParam(value = "selectType",required = false,defaultValue = "radio") String selectType,
                                 @RequestParam(value = "subjectIds", required = false, defaultValue = "") String subjectIds){
        model.addAttribute("selectType",selectType);
        model.addAttribute("subjectIds", subjectIds);
        return resultPage("/course/book/select_book_list");
    }

    /**
     * 加载图书列表数据
     * @param model
     * @param params 查询条件
     * @param currentPage 页码
     * @return 列表页面
     */
    @RequestMapping("/selectBookListPage")
    public String selectBookListPage(Model model,
                               @RequestBody Map<String, Object> params,
                               @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String,Object> dataMap = bookService.findBookPage(params,currentPage,true,false);
        model.addAttribute("dataMap",dataMap);
        model.addAttribute("selectType",params.get("selectType"));
        return resultPage("/course/book/select_book_ajax_list");
    }

    /**
     * 加载图书列表数据
     * @param model
     * @param params 查询条件
     * @param currentPage 页码
     * @return 列表页面
     */
    @RequestMapping("/ajaxBookList")
    public String ajaxBookList(Model model,
                               @RequestBody Map<String, Object> params,
                               @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String,Object> dataMap = bookService.findBookPage(params,currentPage,true,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/course/book/ajax_book_list");
    }

    /**
     * 添加/修改图书
     * @param id 图书ID
     * @return 操作页面
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "id",required = false,defaultValue = "0") BigDecimal id){
        Map<String,Object> book = null;
        if(DataUtil.idIsNotNull(id)){
            book = bookService.findBookById(id,false,true);
        }
        if(ObjectUtils.isEmpty(book)){
            book = new HashMap<>();
        }
        model.addAttribute("book",book);
        return resultPage("/course/book/save_book");
    }

    /**
     * 保存图书数据
     * @param book 图书数据对象
     * @return  Map<String,Object>
     */
    @RequestMapping("/saveBook")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER,action = "保存或修改图书数据")
    public Map<String,Object> saveBook(@RequestBody Map<String,Object> book,
                                       @RequestParam(value = "type",required = false,defaultValue = "1") int type){
        if(type==2){
            book.put("status",1);
        }
        return bookService.saveBook(book);
    }

    /**
     * 修改图书状态
     * @param ids 图书ID串
     * @param status 状态，0初始（可真删除），1上架，2下架，3删除
     * @return Map<String,Object>
     */
    @RequestMapping("/updateBookStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER,action = "修改图书状态")
    public Map<String,Object> updateBookStatus(@RequestParam("ids") String ids,@RequestParam("status") int status){
        return bookService.updateBookStatus(ids,status);
    }

    /**
     * 查看图书详情
     * @param model
     * @param id 图书ID
     * @return
     */
    @RequestMapping("/bookInfo")
    public String bookInfo(Model model,
                           @RequestParam("id") BigDecimal id){
        Map<String,Object> book = bookService.findBookById(id,true,true);
        model.addAttribute("book",book);
        return resultPage("/course/book/book_info");
    }
}
